package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhShippingTemplate;
import com.ruoyi.edh.service.IEdhShippingTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订货-运费模板Controller
 *
 * @author xxx
 * @date 2020-09-03
 */
@RestController
@RequestMapping("/edh/template")
@Api(description = "易订货-运费模板")
public class EdhShippingTemplateController extends BaseController {
    @Autowired
    private IEdhShippingTemplateService edhShippingTemplateService;

    /**
     * 查询易订货-运费模板列表
     */
    @GetMapping("/list")
    @ApiOperation("易订货-运费模板列表")
    public TableDataInfo list(EdhShippingTemplate edhShippingTemplate) {
        startPage();
        List<EdhShippingTemplate> list = edhShippingTemplateService.selectEdhShippingTemplateList(edhShippingTemplate);
        return getDataTable(list);
    }

    /**
     * 导出易订货-运费模板列表
     */
    @Log(title = "易订货-运费模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-运费模板列表")
    public AjaxResult export(EdhShippingTemplate edhShippingTemplate) {
        List<EdhShippingTemplate> list = edhShippingTemplateService.selectEdhShippingTemplateList(edhShippingTemplate);
        ExcelUtil<EdhShippingTemplate> util = new ExcelUtil<EdhShippingTemplate>(EdhShippingTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取易订货-运费模板详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("获取易订货-运费模板详细信息")
    public AjaxResult getInfo(@PathVariable(value = "id") Long id) {
        return AjaxResult.success(edhShippingTemplateService.selectEdhShippingTemplateById(id));
    }


    /**
     * 新增易订货-运费模板
     */
    @Log(title = "易订货-运费模板", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订货-运费模板")
    public AjaxResult add(@RequestBody EdhShippingTemplate edhShippingTemplate) {
        return toAjax(edhShippingTemplateService.insertEdhShippingTemplate(edhShippingTemplate));
    }

    /**
     * 修改易订货-运费模板
     */
    @Log(title = "易订货-运费模板", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订货-运费模板")
    public AjaxResult edit(@RequestBody EdhShippingTemplate edhShippingTemplate) {
        return toAjax(edhShippingTemplateService.updateEdhShippingTemplate(edhShippingTemplate));
    }

    /**
     * 删除易订货-运费模板
     */
    @Log(title = "易订货-运费模板", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation("删除易订货-运费模板")
    public AjaxResult remove(Long[] ids) {
        return toAjax(edhShippingTemplateService.deleteEdhShippingTemplateByIds(ids));
    }

    /**
     * 根据供应商id 和订单金额查找运费模版
     *
     * @param id
     * @param shippingAmount
     * @return
     */
    @GetMapping("/selectShippingAmount")
    @ApiOperation("查询供应商-运费模板")
    public AjaxResult selectShippingAmount(Long id, String shippingAmount) {
        EdhShippingTemplate edhShippingTemplate = edhShippingTemplateService.selectShippingAmount(id, shippingAmount);
        if (null != edhShippingTemplate) {
            return AjaxResult.success(edhShippingTemplate);
        }
        // 没有设置运费模板给默认包邮模板
        return AjaxResult.success(edhShippingTemplateService.selectEdhShippingTemplateById(0L));
    }
}
